package fragment;

import android.animation.ObjectAnimator;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.example.administrator.animation_demo.R;

/**
 * Created by eyesee on 2017/5/8.
 */

public class AlphaFragment extends Fragment implements View.OnClickListener {

    private View mView;
    private Button mBtStart;
    private Button mBtStop;
    private ImageView mIv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mView = View.inflate(getActivity(), R.layout.fragment_alpha, null);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initView(mView);
        return mView;
    }

    private void initView(View view) {
        mIv = ((ImageView) view.findViewById(R.id.iv));
        mBtStart = ((Button) view.findViewById(R.id.al_start));
        mBtStop = ((Button) view.findViewById(R.id.al_stop));

        mBtStart.setOnClickListener(this);
        mBtStop.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.al_start:
                AlphaStart(mIv);
                break;
            case R.id.al_stop:
                AlphaStop(mIv);
                break;
        }
    }

    public void AlphaStart(View view){
        ObjectAnimator animator = ObjectAnimator.ofFloat(view,"alpha",1,0.2f,0.8f,0);
        animator.setDuration(2000);
        animator.start();
    }

    public void AlphaStop(View view){
        ObjectAnimator animator = ObjectAnimator.ofFloat(view,"alpha",0,1,0.5f);
        animator.setDuration(2000);
        animator.start();
    }
}
